package com.dao.impl;

import com.bean.Payment;
import com.dao.PaymentDAO;
import com.util.C3p0Utils;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.SQLException;
import java.util.List;

public class PaymentDAOImpl implements PaymentDAO {

    /**
     * BeanListHandler将结果列表封装
     *查询账单所有信息
     * @return
     * @throws SQLException
     */
    @Override
    public List<Payment> selectAll() throws SQLException {

        return C3p0Utils.qr.query("select * from payment", new BeanListHandler<Payment>(Payment.class));
    }
    /**
     * BeanHandler将单个列表封装
     *根据缴费用户和缴费类型得到缴费信息
     * @param
     * @return
     * @throws SQLException
     */
    @Override
    public List<Payment> selectById(String ownerId,String paymentType) throws SQLException {
        return C3p0Utils.qr.query("select * from payment  where ownerId =? and paymentType =?", new BeanListHandler<>(Payment.class), ownerId,paymentType);
    }
    @Override
    public List<Payment> selectByName(String ownerId) throws SQLException {
        return C3p0Utils.qr.query("select * from payment where ownerId =?", new BeanListHandler<>(Payment.class), ownerId);
    }
    @Override
    public boolean deleteById(Integer id) throws SQLException {
        return C3p0Utils.qr.update("delete from payment where paymentId=?", id) > 0;
    }
    @Override
    public boolean deleteAll(String[] idsArr) throws SQLException {
        StringBuffer sql = new StringBuffer("delete from payment where paymentId in (");
        for (int i = 0; i < idsArr.length; i++) {
            sql.append("?,");
        }
        sql.deleteCharAt(sql.length() - 1);
        sql.append(")");
        return C3p0Utils.qr.update(sql.toString(), idsArr) > 0;
    }
    @Override
    public boolean insert(Payment p) throws SQLException {
        return C3p0Utils.qr.update("insert into payment values(null,?,?,?,?,?,?,?,?,?)", p.getOwnerId(),p.getOrderId(),p.getPaymentType(),p.getPaymentUnit(),
                p.getPaymentNum(),p.getPaymentPrice(),p.getPaymentBalance(),p.getPaymentTime(),p.getPaymentStatus()) > 0;
    }

    /**
     * ScalarHandler用来将单个Object类型封装
     *
     * @return
     * @throws SQLException
     */
    @Override
    public Integer count() throws SQLException {
        int n = C3p0Utils.qr.query("select count(*) from payment", new ScalarHandler<Long>()).intValue();
        return n;
    }
    @Override
    public Integer countSearch(String search) throws SQLException {
        int n = C3p0Utils.qr.query("select count(*) from payment where concat(ownerId,orderId,paymentType,paymentUnit,paymentNum,paymentPrice,paymentBalance," +
                "paymentTime,paymentStatus) like ?", new ScalarHandler<Long>(), "%" + search + "%").intValue();
        return n;
    }
    @Override
    public List<Payment> selectAllPagination(Integer offset, Integer limit) throws SQLException {
        return C3p0Utils.qr.query("select * from payment limit ?,?", new BeanListHandler<>(Payment.class), offset, limit);

    }
    @Override
    public List<Payment> selectAllPaginationSearch(Integer offset, Integer limit, String search) throws SQLException {
        return C3p0Utils.qr.query("select * from payment where concat(ownerId,orderId,paymentType,paymentUnit,paymentNum,paymentPrice,paymentBalance,paymentTime," +
                "paymentStatus) like ? limit ?,?", new BeanListHandler<>(Payment.class), "%"+search+"%", offset, limit);

    }

}
